package com.gy.room;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import com.google.common.collect.Maps;

import io.netty.channel.ChannelHandlerContext;
import message.LoginMessage;
import messageControl.userStatesEnum;

/***
 * 鐢ㄦ潵淇濆瓨鐢ㄦ埛鐨勭姸鎬�
 * 1锛氱櫥鍏�
 * 2.鐧诲嚭
 * 
 * @author gengyu
 *
 */
public class userManager {
	private static final userManager instance = new userManager();
    public static userManager getInstance() {
        return instance;
    }
    
    private Map<Integer,clientPlayInfo> userMap = Maps.newConcurrentMap();

    public clientPlayInfo createNewUser(ChannelHandlerContext ctx,LoginMessage user)
    {
    	clientPlayInfo tmp=new clientPlayInfo();
    	tmp.setCtx(ctx);
    	tmp.setHeadUrl(user.getHeadUrl());
    	tmp.setName(user.getName());
    	tmp.setRoomId(-1);
    	tmp.setUserId(randomUserId());
    	tmp.setSex(user.getSex());
    	add(tmp);
    	return tmp;
    }
    
    public void usrLoginOut()
    {
    	//判斷玩家狀態
    }
    
    /*判断一下连接超时*/
    public void connectTimeOut()
    {
    	
    }
    //玩家断掉链接
    public void disConnect(ChannelHandlerContext ctx) {
    //	return userMap.get(userId);
    	
    	clientPlayInfo usrTmp=null;
    	 for (Map.Entry<Integer, clientPlayInfo> entry : userMap.entrySet()) { 
    	     System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
    	     if(entry.getValue().getCtx().equals(ctx))
    	     {
    	    	 System.out.println("find user " + entry.getKey() + ", Value = " + entry.getValue()); 
    	    	 usrTmp=entry.getValue();
    	    	 break;
    	    	// entry.getValue().setOnLine(false);
    	     }
    	    
    	   }
    	 
    	 if(usrTmp!=null)
    	 {
    		 
    		 //开始进行倒计时函数
    		 
    		 
    		 
    		 //玩家赋闲状态，直接将玩家从玩家队列踢出
    		 if(usrTmp.getStates()==userStatesEnum.leavingLine||usrTmp.getStates()==userStatesEnum.online)
    		 {
    			 removeUser(usrTmp);
    			 return;
    		 }
    		 
    		 
    		 //玩家在匹配界面，直接将玩家从该界面踢出
    		 else if(usrTmp.getStates()==userStatesEnum.matchMode)
    		 {
    			 roomManager.getInstance().outWaitringlist(usrTmp);
    			 removeUser(usrTmp);
    			 return;
    		 }
    		 
    		 //玩家在房间内，更改玩家的状态为断线，等到断线次数达到将玩家从房间内踢出
    		 else if(usrTmp.getStates()==userStatesEnum.matchMode)
    		 {
    			 
    			 //其他交给房间
    			 usrTmp.setStates(userStatesEnum.leavingLine);
    			 usrTmp.setDisconnetTime(System.currentTimeMillis());
    			 
    			
    		     
   		    
    			 //加入一个倒计时，判断过了30S是否有连接，如果没有，则直接做断线
    		
    		
    		 }
    		 else
    		 {
      	    	 System.out.println("find user error "); 
    		 }
    		 
    		 
    	 }
    
    }
    
	//清空玩家身上的状态
	public void resetRoomState(clientPlayInfo player)
	{
		player.setRoomId(0);
		player.setStates(0);
		//清空玩家状态
		if(userMap.containsValue(player)==false)
		{
			add(player);
		}
		
	}
    
	//閫氳繃涓�涓狪D鑾峰彇鐜╁娑堟伅
    public clientPlayInfo getUserById(int userId) {
    	return userMap.get(userId);
    }
    
    public void add(clientPlayInfo user) {
    	userMap.put(user.getUserId(), user);
    }
    
    public void removeUser(clientPlayInfo user) {
    	userMap.remove(user.getUserId());
    }

    public int randomUserId() {
    	int roomId = new Random().nextInt(100000)+100000;
    	while(userMap.containsKey(roomId)) {
    		roomId = new Random().nextInt(100000)+100000;
    	}
    	return roomId;
    }
}
